import util from '../../utils/util'
import { EnumUserType } from '../../utils/constant.js'
const app = getApp(),url = app.globalData.url;
Page({
// "navigationStyle":"custom"
  data: {
    title:"东港人社服务大厅",
		truef:true,
		eetruef:true,
		password:'',
		password2:'',
    mobile:'',
		account:'',
		code:'',
		FScode:'',
		// 倒计时参数
		timeStart:60, //倒计时初始值
    timeCur:null, //当前倒计时显示值
    ifTimeIn:false, //是否倒计时中
		success:false,
		blurpaw:false,
		blurps:false,
		blurpaw2:false,
		blurps2:false,
		blurts:false,
		clurts:false,
		pawatypism:false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
		let userInfo = wx.getStorageSync('userInfo')
    console.log('地址参数',userInfo)
    that.setData({mobile:userInfo.mobileNum.replace(/\s+/g,""),account:userInfo.account})
  },
  onShow(){
		
	},
  // 获取手机号
  setMobile(e){
    let that = this
		that.setData({
			mobile : e.detail.value.replace(/\s+/g,"")//去空格
		});
  },
  setCode(e){
    let that = this
		that.setData({
      code : e.detail.value.replace(/\s+/g,""),
		});
  },
	setNewpos(e){
    let that = this
		that.setData({
      password : e.detail.value.replace(/\s+/g,""),
		});
	},
	setNewpos2(e){
    let that = this
		that.setData({
      password2 : e.detail.value.replace(/\s+/g,""),
		});
	},
  // 倒计时
	setTime(){
		let timeCur = this.data.timeCur - 1;
		// console.log(timeCur);
		if(timeCur < 0){
			clearInterval(this.data.timer);
			this.setData({
				ifTimeIn:false
			});
			return false;
		}
		this.setData({
			timeCur : timeCur
		});
  },
  // 获取验证码
	getMobileVerify(){
		var that = this
		if(!that.data.mobile){
			wx.showModal({
				title: '友情提示',
				content: '请输入手机号',
				showCancel: false,
			});
			return false
    }
		if(!/^1\d{10}$/.test(that.data.mobile)){
			wx.showModal({
				title: '友情提示',
				content: '请输入正确的手机号',
				showCancel: false,
			});
			return false;
		}
		wx.showLoading({
		  title: "发送中",
		  mask: true
		});
    that.setData({
      timeCur : that.data.timeStart,
      timer : setInterval(that.setTime,1000),
      ifTimeIn : true,
      success:false
    });
    
		wx.request({
      url:url+'sms/sandMassage',
      method: "POST",
      data:{
				phoneNumber:that.data.mobile
			},
      success:function(res){
        var ret = res.data
        if(ret.code == 200){
					var str = ret.data;
					var FScode = str;
					that.setData({
						timeCur : that.data.timeStart,
						timer : setInterval(that.setTime,1000),
						ifTimeIn : true,
						success:false,
						FScode:FScode
					});
					wx.hideLoading();
					wx.showToast({
            title: '发送成功',
            icon:'none'
					})
        }else{
          wx.showToast({
            title: '发送失败',
            icon:'none'
					})
					wx.hideLoading();
        }
      }
    })
  },
  // 确定提交
	bindDo(){
		var that = this
		that.setData({ success:false})
		var FScode = that.data.FScode
		if(that.data.password.length > 0 && that.data.password2.length > 0 && (that.data.password != that.data.password2)){
			wx.showToast({
        title: '两次密码输入不一致',
        icon: 'none',
        duration: 2000
      });
      wx.hideLoading()
      return false
		}
		console.log(!that.data.code,that.data.code,FScode)
		if(!that.data.code || FScode != that.data.code){
			wx.showModal({
				title: '友情提示',
				content: '请输入正确验证码',
				showCancel: false,
			});
			that.setData({ success:true,})
			return false;
		}
		wx.request({
			url:url+'enterprise/updatePassword',//up_open_id_by_phone//user_login
			method: "PUT",
			data:{
				resource: '1',
				smsCode: that.data.code,
				account:that.data.account,
				password: util.strEncode(that.data.password),
				mobileNum:that.data.mobile,
			},
			success:function(resh){
				console.log('更新密码',resh)
				if(resh.data.code == 200){
					wx.setStorageSync('userType', EnumUserType.QiYe)
					wx.showToast({
						title: resh.data.msg,
						icon: 'none',
						duration: 1500
					});
					wx.switchTab({
						url: '/pages/my/my',
				})
					wx.showToast({
						title: resh.data.msg,
						icon:'none',
						duration: 1500
					})
				}
			}
		})
	},
  // 手机号验证
	phoneOnBlur(e){
		var that = this
		var blur = e.detail.value
		if(blur == ''){
			console.log('1')
			that.setData({blurts:true})
		}else{
			that.setData({blurts:false})
		}
	},
	// 验证码验证
	codeOnBlur(e){
		var that = this
		var clur = e.detail.value
		if(clur == ''){
			console.log('1')
			that.setData({clurts:true})
		}else{
			that.setData({clurts:false})
		}
		if(that.data.code.length > 0 && that.data.password.length > 0 && that.data.password2.length > 0 ){
			that.setData({ success:true})
		}
	},
	//验证新密码
	codeOnpaw(e){
		var that = this
		var blurpsval = e.detail.value
		var myreg = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,16}$/
		var re = new RegExp(myreg)
		if (!re.test(blurpsval)) {
			that.setData({blurpaw:true})
		}else{
			that.setData({blurpaw:false})
		}
		if(blurpsval == ''){
			console.log('1')
			that.setData({blurps:true})
		}else{
			that.setData({blurps:false})
		}
		if(that.data.code.length > 0 && that.data.password.length > 0 && that.data.password2.length > 0 ){
			that.setData({ success:true})
		}
	},
	//验证重复新密码
	codeOnpaw2(e){
		var that = this
		var blurps2val = e.detail.value
		var myreg = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,16}$/
		var re = new RegExp(myreg)
		if (!re.test(blurps2val)) {
			that.setData({blurpaw2:true})
		}else{
			that.setData({blurpaw2:false})
		}
		if(blurps2val == ''){
			console.log('1')
			that.setData({blurps2:true})
		}else{
			that.setData({blurps2:false})
		}
		if(that.data.code.length > 0 && that.data.password.length > 0 && that.data.password2.length > 0 ){
			that.setData({ success:true})
		}
	},
	caonima(){
		this.setData({
			truef:!this.data.truef
		})
	},
	caonimass(){
		this.setData({
			eetruef:!this.data.eetruef
		})
	},

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  // onReady: function () {

  // },

  /**
   * 生命周期函数--监听页面显示
   */
  // onShow: function () {

  // },

  /**
   * 生命周期函数--监听页面隐藏
   */
  // onHide: function () {

  // },

  /**
   * 生命周期函数--监听页面卸载
   */
  // onUnload: function () {

  // },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  // onPullDownRefresh: function () {

  // },

  /**
   * 页面上拉触底事件的处理函数
   */
  // onReachBottom: function () {

  // },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // }
})